<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-header">
        <h1>研究生奖学金管理系统</h1>
        <p>请登录您的账户</p>
      </div>
      
      <div class="login-form">
        <div class="form-group">
          <label for="username">学号/工号</label>
          <input 
            id="username"
            v-model="username" 
            type="text" 
            placeholder="请输入学号或工号" 
            required 
            class="form-input"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            id="password"
            v-model="password" 
            type="password" 
            placeholder="请输入密码" 
            required 
            class="form-input"
          />
        </div>
        
        <div class="form-group">
          <label for="role">用户类型</label>
          <select 
            id="role"
            v-model="role" 
            class="form-select"
          >
            <option value="student">学生</option>
            <option value="professor">导师</option>
          </select>
        </div>
        
        <button 
          @click="login" 
          :disabled="loading"
          class="login-btn"
        >
          {{ loading ? '登录中...' : '登录' }}
        </button>
        
        <div v-if="error" class="error-message">
          {{ error }}
        </div>
      </div>
      
      <div class="login-footer">
        <p>© 2024 研究生奖学金管理系统</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'

const username = ref('')
const password = ref('')
const role = ref('student')
const error = ref('')
const loading = ref(false)
const router = useRouter()

const studentId = localStorage.getItem('studentId')
const grade = localStorage.getItem('grade')
const scholarships = ref([])

const login = async () => {
  if (!username.value || !password.value) {
    error.value = '请填写完整的登录信息'
    return
  }
  
  loading.value = true
  error.value = ''
  
  try {
    const response = await axios.post('/auth/login', {
      username: username.value,
      password: password.value,
      role: role.value
    })
    
    if (response.data.role === 'student') {
      localStorage.setItem('token', 'student-token')
      localStorage.setItem('role', 'student')
      localStorage.setItem('studentId', response.data.studentId)
      localStorage.setItem('name', response.data.name)
      localStorage.setItem('grade', response.data.grade)
      router.push('/student/dashboard')
    } else {
      localStorage.setItem('token', 'professor-token')
      localStorage.setItem('role', 'professor')
      router.push('/professor/dashboard')
    }
  } catch (e) {
    console.error('登录错误:', e)
    if (e.response && e.response.data) {
      error.value = e.response.data
    } else {
      error.value = '登录失败，请检查网络连接或联系管理员'
    }
  } finally {
    loading.value = false
  }
}

onMounted(async () => {
  // 例如根据年级获取可申请的奖学金
  const res = await axios.get(`/scholarships?grade=${grade}`)
  scholarships.value = res.data
})
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  color: #333;
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.form-input,
.form-select {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 14px;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus,
.form-select:focus {
  outline: none;
  border-color: #667eea;
}

.form-input::placeholder {
  color: #999;
}

.login-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  margin-bottom: 20px;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.error-message {
  background: #fee;
  color: #c53030;
  padding: 12px;
  border-radius: 8px;
  font-size: 14px;
  text-align: center;
  border: 1px solid #feb2b2;
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #e1e5e9;
}

.login-footer p {
  color: #999;
  font-size: 12px;
  margin: 0;
}

@media (max-width: 480px) {
  .login-container {
    padding: 30px 20px;
  }
  
  .login-header h1 {
    font-size: 20px;
  }
}
</style>